package cn.iocoder.yudao.module.train.controller.admin.picture;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.train.controller.admin.picture.vo.*;
import cn.iocoder.yudao.module.train.dal.dataobject.picture.PictureDO;
import cn.iocoder.yudao.module.train.service.picture.PictureService;

@Tag(name = "管理后台 - 轮播图")
@RestController
@RequestMapping("/train/picture")
@Validated
public class PictureController {

    @Resource
    private PictureService pictureService;

    @PostMapping("/create")
    @Operation(summary = "创建轮播图")
    @PreAuthorize("@ss.hasPermission('train:picture:create')")
    public CommonResult<Long> createPicture(@Valid @RequestBody PictureSaveReqVO createReqVO) {
        return success(pictureService.createPicture(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新轮播图")
    @PreAuthorize("@ss.hasPermission('train:picture:update')")
    public CommonResult<Boolean> updatePicture(@Valid @RequestBody PictureSaveReqVO updateReqVO) {
        pictureService.updatePicture(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除轮播图")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('train:picture:delete')")
    public CommonResult<Boolean> deletePicture(@RequestParam("id") Long id) {
        pictureService.deletePicture(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除轮播图")
                @PreAuthorize("@ss.hasPermission('train:picture:delete')")
    public CommonResult<Boolean> deletePictureList(@RequestParam("ids") List<Long> ids) {
        pictureService.deletePictureListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得轮播图")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('train:picture:query')")
    public CommonResult<PictureRespVO> getPicture(@RequestParam("id") Long id) {
        PictureDO picture = pictureService.getPicture(id);
        return success(BeanUtils.toBean(picture, PictureRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得轮播图分页")
    @PreAuthorize("@ss.hasPermission('train:picture:query')")
    public CommonResult<PageResult<PictureRespVO>> getPicturePage(@Valid PicturePageReqVO pageReqVO) {
        PageResult<PictureDO> pageResult = pictureService.getPicturePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PictureRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出轮播图 Excel")
    @PreAuthorize("@ss.hasPermission('train:picture:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPictureExcel(@Valid PicturePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PictureDO> list = pictureService.getPicturePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "轮播图.xls", "数据", PictureRespVO.class,
                        BeanUtils.toBean(list, PictureRespVO.class));
    }

}